package com.hmall.order.web;

import com.hmall.common.dto.Address;
import com.hmall.common.dto.Item;
import com.hmall.common.dto.User;
import com.hmall.common.feigin.ItemClient;
import com.hmall.common.feigin.UserClient;
import com.hmall.order.mapper.OrderDetailMapper;
import com.hmall.order.mapper.OrderLogisticsMapper;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDetail;
import com.hmall.order.pojo.OrderDto;
import com.hmall.order.pojo.OrderLogistics;
import com.hmall.order.service.IOrderService;
import com.hmall.order.untils.ThreadLocalUntils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("order")
@Slf4j
@Transactional
public class OrderController {

   @Autowired
   private IOrderService orderService;
   @Autowired
   UserClient userClient;
   @Autowired
   ItemClient itemClient;
   @Autowired
   OrderDetailMapper orderDetailMapper;
   @Autowired
   OrderLogisticsMapper orderLogisticsMapper;


   @GetMapping("{id}")
   public Order queryOrderById(@PathVariable("id") Long orderId, HttpServletRequest request) {
      Order order = orderService.getById(orderId);
      //查看订单属于哪个用户的【用户信息】
      //要基于feign进行远程调用，是不经过网关【authorization请求头，在userservice中拿不到】
      //如何实现，？只需要经过feign调用时，为其增加authorization请求头
      User user = userClient.findById(order.getUserId());
      String authorization = request.getHeader("authorization");
      log.warn("authorization---->:{}",authorization);
      return order;
   }

   /**
    * 保存订单
    * @param orderDto
    * @return
    */
   @PostMapping
   @Transactional
   public Long creatOrder(@RequestBody OrderDto orderDto){
      String userId = ThreadLocalUntils.getUserId();
      //获得支付方式
      Integer paymentType = orderDto.getPaymentType();
      //购买数量
      Integer num = orderDto.getNum();
      //商品id
      Long itemId = orderDto.getItemId();
      //发货地址id
      Long addressId = orderDto.getAddressId();

      Item item = itemClient.getById(itemId);

      //1，保存订单{order}
      Order order = new Order();
      order.setUserId(Long.valueOf(userId));
      order.setPaymentType(paymentType);
      order.setStatus(1);
      order.setTotalFee(item.getPrice()*num);
      orderService.save(order);

      //2保存订单明细{orderDetail}
      OrderDetail orderDetail = new OrderDetail();
      orderDetail.setItemId(itemId);
      orderDetail.setOrderId(order.getId());
      orderDetail.setName(item.getName());
      orderDetail.setNum(num);
      orderDetail.setSpec(item.getSpec());
      orderDetail.setImage(item.getImage());
      orderDetail.setPrice(item.getPrice());
      orderDetailMapper.insert(orderDetail);

      //3,保存物流信息 ｛adress｝
      Address address = userClient.findAddressById(addressId);
      OrderLogistics orderLogistics = new OrderLogistics();
      orderLogistics.setOrderId(order.getId());
      BeanUtils.copyProperties(address, orderLogistics);
      orderLogisticsMapper.insert(orderLogistics);

      //4,扣减库存
      itemClient.updateStock(itemId, num);

      //返回订单Id
      return order.getId();
   }
}
